﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using Maticsoft.Common;

using System.IO;
using System.Collections;
using System.Xml;

namespace Admin.WorkFlow.FormTable.TaskBook
{
    public partial class TaskBook_Modify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["OrganizeId"] != null && Request.Params["OrganizeId"].ToString() != "")
                {
                    int organizeId = ChinaSoft.Common.Input.SetInt(Request.Params["OrganizeId"]);
                    BindDepartMent(organizeId);
                }
                if (Request.Params["ProjectId"] != null && Request.Params["ProjectId"].ToString() != "")
                {
                    int projectId = ChinaSoft.Common.Input.SetInt(Request.Params["ProjectId"]);
                    BindProjectInfoByProjectId(projectId);
                    BindEmployee();
                }
                BindXieBanDepartMent();
                if (Request.Params["id"] != null && Request.Params["id"].ToString() != "")
                {
                    int Id = ChinaSoft.Common.Input.SetInt(Request.Params["id"]);
                    BindInfo(Id);
                }

            }
        }
        /// <summary>
        /// 绑定项目任务书基本信息
        /// </summary>
        /// <param name="ID"></param>
        private void BindInfo(int ID)
        {
            ChinaSoft.Model.Model_tb_MidSoft_TaskBook modelTaskBook = new ChinaSoft.Model.Model_tb_MidSoft_TaskBook();
            ChinaSoft.BLL.BLL_tb_MidSoft_TaskBook bllTaskBook = new ChinaSoft.BLL.BLL_tb_MidSoft_TaskBook();
            modelTaskBook = bllTaskBook.GetModel(ID);
            if (modelTaskBook != null)
            {
                txtMainNumber.Text = modelTaskBook.MainNumber;
                txtSubNumber.Text = modelTaskBook.SubNumber;
                txtIssueTime.Value = Convert.ToDateTime(modelTaskBook.IssueTime).ToShortDateString();
                if (modelTaskBook.ProjectType == "0")
                    rbtnA.Checked = true;
                else
                    rbtnB.Checked = true;
                ddlIssue.SelectedValue = ChinaSoft.Common.Input.SetString(modelTaskBook.According);
                ddlUnderTakeDepartment.SelectedValue = modelTaskBook.UnderDepartMentId.ToString();
                txtContractMoney.Text = ChinaSoft.Common.Input.SetString(modelTaskBook.ContractMoney);
                #region 设计阶段
                switch (modelTaskBook.Stage)
                {
                    case "1":
                        CheckBox1.Checked = true;
                        break;
                    case "2":
                        CheckBox2.Checked = true;
                        break;
                    case "3":
                        CheckBox3.Checked = true;
                        break;
                    case "4":
                        CheckBox4.Checked = true;
                        break;
                    case "5":
                        CheckBox5.Checked = true;
                        break;
                    case "6":
                        CheckBox6.Checked = true;
                        break;
                    case "7":
                        CheckBox7.Checked = true;
                        break;
                    case "8":
                        CheckBox8.Checked = true;
                        break;
                    case "9":
                        CheckBox9.Checked = true;
                        break;
                    case "10":
                        CheckBox10.Checked = true;
                        break;
                }
                #endregion
                ddlJingBanMan.SelectedValue = ChinaSoft.Common.Input.SetString(modelTaskBook.JingBanId);
                ddlMakeDevelop.SelectedValue = ChinaSoft.Common.Input.SetString(modelTaskBook.MakeDevelopId);
                ddlEngieer.SelectedValue = ChinaSoft.Common.Input.SetString(modelTaskBook.EngineerId);
                ddlYuanZhang.SelectedValue = ChinaSoft.Common.Input.SetString(modelTaskBook.SubDeanId);
                txtEndTime.Value = Convert.ToDateTime(modelTaskBook.EndTime).ToShortDateString();
                txtEndTimeDescription.Text = modelTaskBook.EndTimeDescription;
                txtDescription.Text = modelTaskBook.Description;
                ChinaSoft.Model.Model_tb_MidSoft_XieBanDepartMent modelXieBan = new ChinaSoft.Model.Model_tb_MidSoft_XieBanDepartMent();
                ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent bllXieBan = new ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent();
                int taskBookId=ChinaSoft.Common.Input.SetInt(Request.Params["id"]);
                modelXieBan = bllXieBan.GetModelList("TaskBookId=" + taskBookId + " and DepartMentType=0")[0];
                if (modelXieBan != null)
                {
                    ddlMainDepartment.SelectedValue = modelXieBan.DepartMentId.ToString();
                    HidMainXieBanDepartMent.Value = modelXieBan.XieBanId.ToString();
                    txtMainMoney.Text = modelXieBan.OutPut.ToString();
                    lblMainMen.Text=GetUserName(taskBookId, modelXieBan.XieBanId);
                }
            }
        }
        private string GetUserName(int TaskBookId, int XieBanId)
        {
            string strUserName = string.Empty;
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds = bllBind.GetDataSet("*", "V_SubContractUserLook", "TaskBookId=" + TaskBookId + " and  XieBanId=" + XieBanId);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    strUserName += ds.Tables[0].Rows[i]["UserName"].ToString() + "  ";
                }
            }
            return strUserName;
        }
        /// <summary>
        /// 根据项目编号绑定项目信息
        /// </summary>
        /// <param name="projectId"></param>
        private void BindProjectInfoByProjectId(int projectId)
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo bllProject = new ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo();
            ChinaSoft.Model.Model_tb_MidSoft_ProjectInfo modelProject = new ChinaSoft.Model.Model_tb_MidSoft_ProjectInfo();
            modelProject = bllProject.GetModel(projectId);
            if (modelProject != null)
            {
                lblProjectName.Text = modelProject.ProjectName;
                lblCustomer.Text = modelProject.Customer;
                lblProjectNumber.Text = modelProject.ProjectNumber;
            }
        }
        /// <summary>
        /// 绑定部门信息
        /// </summary>
        /// <param name="organizeId">委托单位ID</param>
        private void BindDepartMent(int organizeId)
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_DepartMent bllDepartMent = new ChinaSoft.BLL.BLL_tb_MidSoft_DepartMent();
            DataSet ds = bllDepartMent.GetList("OrganizeId=" + organizeId);
            ChinaSoft.Common.BindControl.BindDropDownList(ddlUnderTakeDepartment, ds, "DepartMentName", "DepartMentId");
            ChinaSoft.Common.BindControl.BindDropDownList(ddlMainDepartment, ds, "DepartMentName", "DepartMentId");
        }
        /// <summary>
        /// 绑定员工信息
        /// </summary>
        private void BindEmployee()
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_Users bllUsers = new ChinaSoft.BLL.BLL_tb_MidSoft_Users();
            DataSet ds = bllUsers.GetAllList();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                #region 主办部门项目负责人
                ChinaSoft.Common.BindControl.BindDropDownList(ddlMainMan, ds, "UserName", "UsersId");
                #endregion

                #region 经办人
                ChinaSoft.Common.BindControl.BindDropDownList(ddlJingBanMan, ds, "UserName", "UsersId");
                #endregion

                #region 生产发展部
                ChinaSoft.Common.BindControl.BindDropDownList(ddlMakeDevelop, ds, "UserName", "UsersId");
                #endregion

                #region 总工程师室
                ChinaSoft.Common.BindControl.BindDropDownList(ddlEngieer, ds, "UserName", "UsersId");
                #endregion

                #region 分管院长
                ChinaSoft.Common.BindControl.BindDropDownList(ddlYuanZhang, ds, "UserName", "UsersId");
                #endregion
            }
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtMainNumber.Text.Trim().Length == 0)
            {
                strErr += "主编号（综合字）不能为空！\\n";
            }
            if (this.txtSubNumber.Text.Trim().Length == 0)
            {
                strErr += "子编号(第N号)不能为空！\\n";
            }
            if (this.ddlUnderTakeDepartment.SelectedItem.Text == "")
            {
                strErr += "承接部门不能为空！\\n";
            }
            if (!PageValidate.IsAllNumber(txtContractMoney.Text))
            {
                strErr += "合同金额格式错误！\\n";
            }
            if (ddlJingBanMan.SelectedItem.Text == "")
            {
                strErr += "经办人未填！\\n";
            }
            if (ddlMakeDevelop.SelectedItem.Text == "")
            {
                strErr += "生产发展部不能为空！\\n";
            }
            if (ddlEngieer.SelectedItem.Text == "")
            {
                strErr += "总工程师室不能为空！\\n";
            }
            if (ddlYuanZhang.SelectedItem.Text == "")
            {
                strErr += "分管院长不能为空！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string MainNumber = this.txtMainNumber.Text;
            string SubNumber = this.txtSubNumber.Text;
            int PmFlowId = ChinaSoft.Common.Input.SetInt(Request.Params["PmFlowId"]);
            DateTime IssueTime = DateTime.Parse(this.txtIssueTime.Value);
            string ProjectType = rbtnA.Checked == true ? "0" : "1";//0=A ,1=B
            int According = ChinaSoft.Common.Input.SetInt(ddlIssue.SelectedItem.Value);
            int UnderTakeDepartMentId = ChinaSoft.Common.Input.SetInt(ddlUnderTakeDepartment.SelectedItem.Value);
            decimal ContractMoney = ChinaSoft.Common.Input.SetDecimal(txtContractMoney.Text.Trim());
            #region 设计阶段
            string Stage = "0";
            if (CheckBox1.Checked)
                Stage = "1";
            else if (CheckBox2.Checked)
                Stage = "2";
            else if (CheckBox3.Checked)
                Stage = "3";
            else if (CheckBox4.Checked)
                Stage = "4";
            else if (CheckBox5.Checked)
                Stage = "5";
            else if (CheckBox6.Checked)
                Stage = "6";
            else if (CheckBox7.Checked)
                Stage = "7";
            else if (CheckBox8.Checked)
                Stage = "8";
            else if (CheckBox9.Checked)
                Stage = "9";
            else if (CheckBox10.Checked)
                Stage = "10";
            #endregion
            int JingBanManId = ChinaSoft.Common.Input.SetInt(ddlJingBanMan.SelectedItem.Value);
            int MakeDevelopId = ChinaSoft.Common.Input.SetInt(ddlMakeDevelop.SelectedItem.Value);
            int EngineerId = ChinaSoft.Common.Input.SetInt(ddlEngieer.SelectedItem.Value);
            int SubDeanId = ChinaSoft.Common.Input.SetInt(ddlYuanZhang.SelectedItem.Value);
            DateTime EndTime = DateTime.Parse(this.txtEndTime.Value);
            string EndTimeDescription = this.txtEndTimeDescription.Text;
            string Description = this.txtDescription.Text;

            ChinaSoft.BLL.BLL_tb_MidSoft_TaskBook bll = new ChinaSoft.BLL.BLL_tb_MidSoft_TaskBook();
            ChinaSoft.Model.Model_tb_MidSoft_TaskBook model =bll.GetModel(ChinaSoft.Common.Input.SetInt(Request.Params["id"]));
            model.JingBanId = JingBanManId;
            model.MainNumber = MainNumber;
            model.SubNumber = SubNumber;
            model.PmFlowId = PmFlowId;
            model.IssueTime = IssueTime;
            model.ProjectType = ProjectType;
            model.According = According;
            model.UnderDepartMentId = UnderTakeDepartMentId;
            model.ContractMoney = ContractMoney;
            model.Stage = Stage;
            model.MakeDevelopId = MakeDevelopId;
            model.EngineerId = EngineerId;
            model.SubDeanId = SubDeanId;
            model.EndTime = EndTime;
            model.EndTimeDescription = EndTimeDescription;
            model.Description = Description;
             bll.Update(model);
            Response.Write("<script>alert('成功！');top.window.parent.frames.item('workspace').location.reload();window.parent.$('#DivSysiframe').dialog(\"close\");</script>");
        }
        /// <summary>
        /// 添加联系人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUnderTakeUserAdd_Click(object sender, EventArgs e)
        {
            ChinaSoft.Model.Model_tb_MidSoft_SubContractUser modelUser = new ChinaSoft.Model.Model_tb_MidSoft_SubContractUser();
            ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent modelXieBanDepartMent = new ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent();
        }
        /// <summary>
        /// 添加主办部门负责人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMainUserAdd_Click(object sender, EventArgs e)
        {
            if (ddlMainMan.SelectedItem.Text != "")
            {
                ChinaSoft.Model.Model_tb_MidSoft_SubContractUser modelMainUser = new ChinaSoft.Model.Model_tb_MidSoft_SubContractUser();
                ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser bllMainUser = new ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser();
                modelMainUser.CreateTime = DateTime.Now;
                modelMainUser.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
                modelMainUser.SubContractUserId = ChinaSoft.Common.Input.SetInt(ddlMainMan.SelectedItem.Value);
                modelMainUser.XieBanId = ChinaSoft.Common.Input.SetInt(HidMainXieBanDepartMent.Value);
                modelMainUser.TaskBookId = 0;
                bllMainUser.Add(modelMainUser);
                lblMainMen.Text += ddlMainMan.SelectedItem.Text + ",";
            }
        }
        /// <summary>
        /// 删除最后一个主办部门项目负责人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMainUserDel_Click(object sender, EventArgs e)
        {
            ChinaSoft.Model.Model_tb_MidSoft_SubContractUser modelMainUser = new ChinaSoft.Model.Model_tb_MidSoft_SubContractUser();
            ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser bllMainUser = new ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser();
            int recordCount = bllMainUser.GetRecordCount("XieBanId=" + HidMainXieBanDepartMent.Value + " and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]));
            if (recordCount > 0)
            {
                ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
                bllBind.DeleteWhere("tb_MidSoft_SubContractUser", "SubContractId=(select   top   1   SubContractId   from   tb_MidSoft_SubContractUser where XieBanId=0 order by SubContractId Desc)  ");
            }
            string[] strArr = lblMainMen.Text.Split(',');
            string str3 = "";
            if (strArr.Length > 0)
            {
                for (int i = 0; i < strArr.Length; i++)
                {
                    if (i >= strArr.Length - 2)
                    {
                        str3 += "";
                    }
                    else
                    {
                        str3 += strArr[i] + ",";
                    }
                }
            }
            lblMainMen.Text = str3;
        }

        protected void ddlMainDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChinaSoft.Model.Model_tb_MidSoft_XieBanDepartMent modelXieBan = new ChinaSoft.Model.Model_tb_MidSoft_XieBanDepartMent();
            ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent bllXieBan = new ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent();
            if (ddlMainDepartment.SelectedItem.Text != "")
            {
                if (HidMainXieBanDepartMent.Value != "")
                {
                    //修改
                    modelXieBan = bllXieBan.GetModelList("TaskBookId=0 and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]))[0];
                    modelXieBan.DepartMentId = ChinaSoft.Common.Input.SetInt(ddlMainDepartment.SelectedItem.Value);
                    bllXieBan.Update(modelXieBan);
                    ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
                    bllBind.UpdateList("XieBanId=" + ChinaSoft.Common.Input.SetInt(ddlMainDepartment.SelectedItem.Value), "tb_MidSoft_SubContractUser", "XieBanId=" + ChinaSoft.Common.Input.SetInt(HidMainXieBanDepartMent.Value) + " and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]));
                    HidMainXieBanDepartMent.Value = ddlMainDepartment.SelectedItem.Value;
                }
                else
                {
                    //添加
                    modelXieBan.CreateTime = DateTime.Now;
                    modelXieBan.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
                    modelXieBan.DepartMentId = ChinaSoft.Common.Input.SetInt(ddlMainDepartment.SelectedItem.Value);
                    modelXieBan.DepartMentType = 0;
                    modelXieBan.OutPut = 0;
                    modelXieBan.TaskBookId = 0;
                    bllXieBan.Add(modelXieBan);
                    HidMainXieBanDepartMent.Value = ddlMainDepartment.SelectedItem.Value;
                }
            }
        }


        private void BindXieBanDepartMent()
        {
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds = bllBind.GetDataSet("*", "V_XieBanDepartMent", " TaskBookId=0 and DepartMentType=1 and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]));
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindXieBanDepartMent();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
                string XieBanId = GridView1.DataKeys[e.Row.RowIndex][0].ToString();
                DataSet ds = bllBind.GetDataSet("*", "V_SubContractUserLook", " XieBanId=" + XieBanId + " and TaskBookId=0 ");
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    Label lblMan = e.Row.FindControl("lblProjectMan") as Label;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        lblMan.Text += ds.Tables[0].Rows[i]["UserName"].ToString() + ",";
                    }
                }
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ID = (int)GridView1.DataKeys[e.RowIndex].Value;
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            bllBind.DeleteWhere("tb_MidSoft_SubContractUser", "TaskBookId=0 and XieBanId=" + ID);
            ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent bll = new ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent();
            bll.Delete(ID);
            Maticsoft.Common.MessageBox.Show(this, "删除成功！");
            BindXieBanDepartMent();
        }
    }
}